package ee.siimkaspar.subjects.service;

import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import ee.siimkaspar.subjects.model.Address;
import ee.siimkaspar.subjects.model.Contact;
import ee.siimkaspar.subjects.model.Person;
import ee.siimkaspar.subjects.model.SubjectAttribute;
import ee.siimkaspar.subjects.util.Const;

@Transactional
public class SearchService extends BaseService {

	@SuppressWarnings("unchecked")
	public List<Person> searchPersons(Person person, Address address, Contact contact, List<SubjectAttribute> attributes) {
		try {
			String likeStart = " like '";
			String likeEnd = "%'";
			StringBuilder sql = new StringBuilder("select distinct p from Person p, Address a, Contact c, SubjectAttribute sa ");
			sql.append("where a.subjectType.id=1 and c.subjectType.id=1 and sa.subjectType.id=1 ");
			if (person.getFirstName() != null && !person.getFirstName().isEmpty()) {
				sql.append(" and upper(p.firstName) " + likeStart + person.getFirstName().toUpperCase() + likeEnd);
			}
			if (person.getLastName() != null && !person.getLastName().isEmpty()) {
				sql.append(" and upper(p.lastName) " + likeStart + person.getLastName().toUpperCase() + likeEnd);
			}
			if (person.getIdentityCode() != null && !person.getIdentityCode().isEmpty()) {
				sql.append(" and upper(p.identityCode) " + likeStart + person.getIdentityCode().toUpperCase() + likeEnd);
			}
			if (contact.getValueText() != null && !contact.getValueText().isEmpty()) {
				sql.append(" and upper(c.valueText) " + likeStart + contact.getValueText().toUpperCase() + likeEnd);
			}
			if (address.getCountry() != null && !address.getCountry().isEmpty()) {
				sql.append(" and upper(a.country) " + likeStart + address.getCountry().toUpperCase() + likeEnd);
			}
			if (address.getCounty() != null && !address.getCounty().isEmpty()) {
				sql.append(" and upper(a.county) " + likeStart + address.getCounty().toUpperCase() + likeEnd);
			}
			if (address.getTownVillage() != null && !address.getTownVillage().isEmpty()) {
				sql.append(" and upper(a.townVillage) " + likeStart + address.getTownVillage().toUpperCase() + likeEnd);
			}
			if (address.getStreetAddress() != null && !address.getStreetAddress().isEmpty()) {
				sql.append(" and upper(a.streetAddress) " + likeStart + address.getStreetAddress().toUpperCase() + likeEnd);
			}
			if (address.getZipcode() != null && !address.getZipcode().isEmpty()) {
				sql.append(" and upper(a.zipcode) " + likeStart + address.getZipcode().toUpperCase() + likeEnd);
			}
			for (SubjectAttribute attribute : attributes) {
				if (attribute.getValueText() != null && !attribute.getValueText().isEmpty() &&
						attribute.getDataType() != null) {
					switch (attribute.getDataType()) {
					case Const.DATA_TYPE_DATE:
						sql.append(" and sa.valueDate="+ attribute.getValueText().toUpperCase());
						break;
					case Const.DATA_TYPE_NUMBER:
						sql.append(" and sa.valueNumber=" + likeStart + attribute.getValueText().toUpperCase() + likeEnd);
						break;
					case Const.DATA_TYPE_STRING:
						sql.append(" and upper(sa.valueText) " + likeStart + attribute.getValueText().toUpperCase() + likeEnd);
						break;

					default:
						break;
					}
					System.out.println(attribute.getValueText() + " - " + attribute.getDataType());
//					sql.append(" and upper(sa.valueText)");
				}
			}
//			p.firstName=:firstName and p.lastName=:lastName
//					"p.identityCode=:identityCode and p.birthDate=:birthDate";
			System.out.println(sql);
			 Query q = getEntityManager().createQuery(sql.toString());
			 return q.getResultList();
		 } catch (Exception e) {
			Logger.getLogger(UserService.class).warn(e.getMessage());
		}
		 return null;
	}
	
}
